﻿using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace UtilZ.Dotnet.Ex.Json
{
    /// <summary>
    /// json序列化设置类
    /// </summary>
    public class JsonSerializeSetting
    {
        private static JsonSerializeSetting _default;
        /// <summary>
        /// 获取默认实例
        /// </summary>
        public static JsonSerializeSetting Default
        {
            get { return _default; }
        }

        static JsonSerializeSetting()
        {
            _default = new JsonSerializeSetting();
        }



        /// <summary>
        /// 序列化成员类型(可选值为:MemberTypes.Property、MemberTypes.Field、MemberTypes.All)
        /// </summary>
        public MemberTypes MemberType { get; set; } = MemberTypes.All;

        /// <summary>
        /// 是否使用序列化后首字母小写,true:首字母小写;false:首字母不小写
        /// </summary>
        public bool UseCamelCasePropertyNames { get; set; } = true;

        /// <summary>
        /// 忽略值为null的属性和字段
        /// </summary>
        public bool IgnoreNullValues { get; set; } = false;

        /// <summary>
        /// 忽略只读字段
        /// </summary>
        public bool IgnoreReadOnlyFields { get; set; } = false;

        /// <summary>
        /// 忽略只读属性
        /// </summary>
        public bool IgnoreReadOnlyProperties { get; set; } = false;

        /// <summary>
        /// 忽略的成员集合,类型.成员
        /// </summary>
        public IEnumerable<string> IgnoreMembers { get; set; } = null;

        /// <summary>
        /// 填充值类型[true:当json值为null时,将该对象属性或字段的值修改为null;false:忽略该值,不将该对象属性或字段的值修改为null]
        /// </summary>
        public bool FillType { get; set; } = false;

        /// <summary>
        /// 是否写入扫描[true:写入到json文件中;false:不写入到json文件中]
        /// </summary>
        public bool WriteDescription { get; set; } = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        public JsonSerializeSetting()
        {

        }


        private string _valueStr = null;
        private string GetValueString()
        {
            if (this._valueStr == null)
            {
                var proInfoArr = this.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                StringBuilder sb = new StringBuilder();
                object proValue;

                foreach (var proInfo in proInfoArr)
                {
                    proValue = proInfo.GetValue(this, null);

                    if (sb.Length > 0)
                    {
                        sb.Append(';');
                    }
                    sb.Append(proInfo.Name);
                    sb.Append(':');
                    sb.Append(proValue);
                }
                this._valueStr = sb.ToString();
            }

            return this._valueStr;
        }

        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns>String</returns>
        public override string ToString()
        {
            return this.GetValueString();
        }

        /// <summary>
        /// 重写Equals
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            var exObj = obj as JsonSerializeSetting;
            if (exObj == null)
            {
                return false;
            }

            if (exObj == this)
            {
                return true;
            }

            string currentValueStr = this.GetValueString();
            string exObjValueStr = exObj.GetValueString();
            return string.Equals(currentValueStr, exObjValueStr);
        }

        /// <summary>
        /// 重写GetHashCode
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.GetValueString().GetHashCode();
        }
    }
}
